package logic

import (
	"context"
	"user/internal/svc"
	"user/models/mysql"
	"user/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LonginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLonginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LonginLogic {
	return &LonginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LonginLogic) Longin(in *user.LonginRequest) (*user.LonginResponse, error) {
	// todo: add your logic here and delete this line
	var err error
	var u mysql.User
	u.Account = in.Account
	u.Password = in.Password
	if err = u.GetUserInfo(); err != nil {
		logx.Error("数据库异常")
		return nil, err
	}
	if u.ID == 0 {
		if err = u.CreateUser(); err != nil {
			logx.Error("数据库异常", err)
			return nil, err
		}
	}
	return &user.LonginResponse{
		Id: int64(u.ID),
	}, nil
}
